﻿using System;
using System.Web.Mvc;
using LogikBug.ModularMvc.Caching;

namespace LogikBug.ModularMvc.Factories
{
	public abstract class ModelFactory : IModelFactory
	{
		#region Fields

		private static readonly IModelFactory DefaultMenuFactoryImpl = new DynamicModelFactory();
		private static IModelFactory _currentMenuFactory;

		#endregion

		#region Constructors

		protected ModelFactory()
		{
			Cache = new ModelCache();
		}

		#endregion

		#region Properties

		public static IModelFactory DefaultModelFactory
		{
			get
			{
				return DefaultMenuFactoryImpl;
			}
		}

		public static IModelFactory Current
		{
			get
			{
				return GetCurrentModelFactory();
			}
		}

		public ModelCache Cache { get; protected set; }

		public IDependencyResolver Resolver
		{
			get
			{
				return DependencyResolver.Current;
			}
		}

		#endregion

		#region Methods

		public static void SetCurrentModelFactory(IModelFactory menuFactory)
		{
			_currentMenuFactory = menuFactory;
		}

		public static IModelFactory GetCurrentModelFactory()
		{
			return _currentMenuFactory ?? DefaultMenuFactoryImpl;
		}

		public virtual T Create<T>()
		{
			return (T)Create(typeof(T));
		}

		public abstract object Create(Type interfaceType);

		#endregion
	}
}
